library(data.table)
library(Hmisc)
library(stringr)
library(cjoint)
library(multiwayvcov)
library(car)
library(pBrackets)
library(survey)

# set working directory to appropriate location
setwd("...")

# read in anonymized data where each row is a respondent-task observation
amce_data <- fread("LDAsurveydata.csv", header = TRUE, stringsAsFactors = FALSE)

# set baseline factor values
amce_data$stage <- factor(amce_data$stage, levels=c("committee", "floor"))
amce_data$pty <- factor(amce_data$pty, levels=c("Republican", "Democrat"))
amce_data$num_const <- factor(amce_data$num_const, levels=c("Very low", "Low", "Moderate", "High"))
amce_data$seniority <- factor(amce_data$seniority, levels=c("Freshman", "1 term", "3 terms", "7 terms"))
amce_data$position <- factor(amce_data$position, levels=c("Oppose", "Undeclared", "Support"))
amce_data$cosponsor <- factor(amce_data$cosponsor, levels=c("No", "Yes"))
amce_data$cmte <- factor(amce_data$cmte, levels=c("No", "Yes"))
amce_data$cmteldr <- factor(amce_data$cmteldr, levels=c("None", "Subcommittee ranking member", "Subcommittee chair"))
amce_data$mov <- factor(amce_data$mov, levels=c("Less than 10%", "10% to 20%", "20% to 30%", "More than 30%"))
amce_data$PAC <- factor(amce_data$PAC, levels=c("No", "Yes; $100", "Yes; $500", "Yes; $1000"))
amce_data$issue <- factor(amce_data$issue, levels=c("ACR", "SCA"))

# read in conjoint design generated by Hainmueller et al application
generic_design <- makeDesign(type="file",
                             filename="design.dat")

# remove respondent-task rows where respondents did not complete binary choice task; 
# amce function cannot handle missingness
amce_data_bin <- amce_data[!which(is.na(amce_data$choice)),]

# use amce function to estimate both AMCEs when pooling across stages and ACIEs for floor and
# committee stages; this model is the basis of Figures 2 and 3 in the main paper and the
# corresponding Tables A.6 and A.7 in the Appendix
results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr + stage +
                  stage:pty + stage:num_const + stage:seniority + stage:position + 
                  stage:cmte + stage:mov + stage:PAC +
                  stage:cosponsor + stage:cmteldr,
                data = amce_data_bin, design = generic_design,
                respondent.id = "respondent", respondent.varying = "stage")
summary(results)

# to make plots, I need the point estimates and confidence intervals for the AMCEs and ACIEs
# that are calculated internally in the plot.amce function and not saved for output; in order
# to obtain the point estimates and confidence intervals, I extract and manipulate the
# code for the cjoint package so that the functions plot.amce.resp.int and plot.amce.attr.int will 
# not yield the default plots, but will instead only calculate and return the point estimates and 
# confidence intervals for the respondent characteristics-condition and the attribute-by-attribute
# condition analyses, respectively
source("AMCE_Funcs.R")

###################################################################################
###################################################################################
###################################################################################

# Appendix

# Descriptives used to create Table A.4

# read in descriptive data for full sample
sample_data <- fread("sample_data.csv", header = TRUE, stringsAsFactors = FALSE)

# respondents have ResponseIDs, non-respondents have NA values

# lobbyist employer (client vs. firm)
# in sampling frame
round(prop.table(table(sample_data$Isfirm)),3)*100
# in responses
round(prop.table(table(sample_data$Isfirm[!is.na(sample_data$ResponseID)])),3)*100

t.test(sample_data$Isfirm[which(!is.na(sample_data$ResponseID))],
       sample_data$Isfirm)

# break down expenditures by quartile

# $ amounts only reported if in excess of 5000, 0 otherwise; bring all non-reported
# values to 5000
sample_data$Amount <- ifelse(sample_data$Amount==0, 5000, sample_data$Amount)

sample_data$AmountQuart <- dplyr::ntile(sample_data$Amount, 4)

# in sampling frame
round(prop.table(table(sample_data$AmountQuart)),3)*100
# in responses
round(prop.table(table(sample_data$AmountQuart[!is.na(sample_data$ResponseID)])),3)*100

t.test(sample_data$Amount[which(!is.na(sample_data$ResponseID))],
       sample_data$Amount)

# CRP categories

# sampling frame
round(prop.table(table(sample_data$CRPcat)),3)*100
# among respondents
round(prop.table(table(sample_data$CRPcat[which(!is.na(sample_data$ResponseID))])),3)*100

chisq.test(table(sample_data$CRPcat, !is.na(sample_data$ResponseID)))

# registered lobbyist?

# sampling frame
round(prop.table(table(sample_data$RegisteredLobbyist)),3)*100
# among respondents
round(prop.table(table(sample_data$RegisteredLobbyist[which(!is.na(sample_data$ResponseID))])),3)*100

t.test(sample_data$RegisteredLobbyist[which(!is.na(sample_data$ResponseID))],
       sample_data$RegisteredLobbyist)

# merge with respondent data for later analysis
amce_data_bin <- merge(amce_data_bin, sample_data, by.x="respondent", by.y = "ResponseID",
                       all.x = TRUE)

# descriptives for Table A.5

resp_data <- fread("resp_data.csv", header = TRUE, stringsAsFactors = FALSE)

# gender
round(prop.table(table(resp_data$Gender, useNA = "always")),3)*100

# age
round(prop.table(table(resp_data$Age, useNA = "always")),3)*100

# education
round(prop.table(table(resp_data$Education, useNA = "always")),3)*100

# race
round(prop.table(table(resp_data$Race, useNA = "always")),3)*100

# income
round(prop.table(table(resp_data$Income, useNA = "always")),3)*100

# ideology
round(prop.table(table(resp_data$Ideology, useNA = "always")),3)*100

# PID
round(prop.table(table(resp_data$PID7, useNA = "always")),3)*100

# years of lobbying experience
round(prop.table(table(resp_data$Years_Experience, useNA = "always")),3)*100

# past government experience
# member of Congress
round(prop.table(table(resp_data$PrevExp_MemCongress, useNA = "always")),3)*100
# congressional staff
round(prop.table(table(resp_data$PrevExp_CongStaff, useNA = "always")),3)*100
# presidential appointee
round(prop.table(table(resp_data$PrevExp_PresApp, useNA = "always")),3)*100
# EOP staffer
round(prop.table(table(resp_data$PrevExp_EOP, useNA = "always")),3)*100
# civil servant
round(prop.table(table(resp_data$PrevExp_Bureaucrat, useNA = "always")),3)*100
# other
round(prop.table(table(resp_data$PrevExp_Other, useNA = "always")),3)*100
# none (i.e., answered no for all other options)
round(prop.table(table(resp_data$PrevExp_None, useNA = "always")),3)*100

# current role with client
round(prop.table(table(resp_data$CurrPosition, useNA = "always")),3)*100

# merge with respondent data for later analysis
amce_data_bin <- merge(amce_data_bin, resp_data, by.x="respondent", by.y = "ResponseID",
                       all.x = TRUE)

# Table A.6 (results from Figure 2)
d <- plot.amce.resp.int(results, ci=0.9975, facet.names = "stage")
cbind(round(d[which(d$facet=="Unconditional" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Unconditional" & d$pe<100),c(5,7)])

# Table A.7 (results from Figure 3)
d <- plot.amce.resp.int(results, ci=0.99875, facet.names = "stage")
cbind(round(d[which(d$facet=="Conditional on\nstage = committee" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nstage = committee" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="Conditional on\nstage = floor" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nstage = floor" & d$pe<100),c(5,7)])

# Table A.8 (analog to Table A.6 with ordinal response)

# remove respondent-task rows where respondents did not complete task; amce function cannot
# handle missingness
amce_data_ordinal <- amce_data[!which(is.na(amce_data$ranking)),]

results <- amce(ranking ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr + stage +
                  stage:pty + stage:num_const + stage:seniority + stage:position + 
                  stage:cmte + stage:mov + stage:PAC +
                  stage:cosponsor + stage:cmteldr
                , data = amce_data_ordinal, design = generic_design,
                respondent.id = "respondent", respondent.varying = "stage")
summary(results)

d <- plot.amce.resp.int(results, ci=0.9975, facet.names = "stage")
cbind(round(d[which(d$facet=="Unconditional" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Unconditional" & d$pe<100),c(5,7)])

# Table A.9 (analog to Table A.7 with ordinal response)
d <- plot.amce.resp.int(results, ci=0.99875, facet.names = "stage")
cbind(round(d[which(d$facet=="Conditional on\nstage = committee" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nstage = committee" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="Conditional on\nstage = floor" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nstage = floor" & d$pe<100),c(5,7)])

# Table A.10 (analog to Table A.6 with weights)

# creating survey weights using distributions of covariates available for full sample
# (lobbyist employer, expenditures, CRP category, registered or not)

final_list_weights <- svydesign(ids=~1, data = sample_data[which(!is.na(sample_data$ResponseID)),])

firm.dist <- data.frame(Isfirm = c("0", "1"),
                        Freq = nrow(final_list_weights) * c(0.53, 0.47))

reglob.dist <- data.frame(RegisteredLobbyist = c("0", "1"),
                          Freq = nrow(final_list_weights) * c(0.38, 0.62))

spend.dist <- data.frame(AmountQuart = c("1", "2", "3", "4"),
                         Freq = nrow(final_list_weights) * c(0.25, 0.25, 0.25, 0.25))

cat.dist <- data.frame(CRPcat = c("Agribusiness", "Communications and Electronics",
                                  "Construction", "Defense", "Energy and Natural Resources",
                                  "Finance, Insurance and Real Estate", 
                                  "Health", "Ideological and Single-Issue",
                                  "Labor", "Lawyers and Lobbyists",
                                  "Misc Business", "Other", "Transportation",
                                  "Unknown"),
                       Freq = nrow(final_list_weights) * c(0.04, 0.07, 0.02, 0.02,
                                                           0.08, 0.10, 0.19, 0.09,
                                                           0.02, 0.01, 0.12, 0.06,
                                                           0.06, 0.12))

final_list_weights.rake <- rake(design = final_list_weights,
                                sample.margins = list(~Isfirm, ~RegisteredLobbyist,
                                                      ~AmountQuart, ~CRPcat),
                                population.margins = list(firm.dist, reglob.dist,
                                                          spend.dist, cat.dist))

survey_weights <- data.frame("respondent"=final_list_weights.rake$variables$ResponseID, 
                             "weight"=final_list_weights.rake$prob)

amce_data_bin <- merge(amce_data_bin, survey_weights, by="respondent", all.x = TRUE)

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr + stage +
                  stage:pty + stage:num_const + stage:seniority + stage:position + 
                  stage:cmte + stage:mov + stage:PAC +
                  stage:cosponsor + stage:cmteldr
                , data = amce_data_bin, design = generic_design,
                respondent.id = "respondent", respondent.varying = "stage", 
                weights = "weight")
summary(results)

d <- plot.amce.resp.int(results, ci=0.9975, facet.names = "stage")
cbind(round(d[which(d$facet=="Unconditional" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Unconditional" & d$pe<100),c(5,7)])

# Table A.11 (analog to Table A.7 with weights)

d <- plot.amce.resp.int(results, ci=0.99875, facet.names = "stage")
cbind(round(d[which(d$facet=="Conditional on\nstage = committee" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nstage = committee" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="Conditional on\nstage = floor" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nstage = floor" & d$pe<100),c(5,7)])

# Table A.12 (issue-conditional effects, check stability and no carryover effects assumption)

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr + issue +
                  issue:pty + issue:num_const + issue:seniority + issue:position + 
                  issue:cmte + issue:mov + issue:PAC +
                  issue:cosponsor + issue:cmteldr
                , data = amce_data_bin, design = generic_design,
                respondent.id = "respondent", respondent.varying = "issue")
summary(results)

d <- plot.amce.resp.int(results, ci=0.99875, facet.names = "issue")
cbind(round(d[which(d$facet=="Conditional on\nissue = ACR" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nissue = ACR" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="Conditional on\nissue = SCA" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nissue = SCA" & d$pe<100),c(5,7)])

# Table A.13 (employer type conditional effects (firm vs. in-house lobbyists))

# amce function needs facet as a factor
amce_data_bin$Isfirm <- ifelse(amce_data_bin$Isfirm==1, "Yes", "No")
amce_data_bin$Isfirm <- as.factor(amce_data_bin$Isfirm) 

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr + Isfirm +
                  Isfirm:pty + Isfirm:num_const + Isfirm:seniority + Isfirm:position + 
                  Isfirm:cmte + Isfirm:mov + Isfirm:PAC +
                  Isfirm:cosponsor + Isfirm:cmteldr, 
                data = amce_data_bin, design = generic_design,
                respondent.id = "respondent", respondent.varying = "Isfirm")
summary(results)

d <- plot.amce.resp.int(results, ci=0.99875, facet.names = "Isfirm")
cbind(round(d[which(d$facet=="Conditional on\nIsfirm = No" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nIsfirm = No" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="Conditional on\nIsfirm = Yes" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nIsfirm = Yes" & d$pe<100),c(5,7)])

# Table A.14 (respondent partisanship conditional effects)

# removing respondents where PID is NA, as amce function cannot handle missingness
amce_data_PID <- amce_data_bin[which(!is.na(amce_data_bin$PID3)),]
amce_data_PID$PID3 <- as.factor(amce_data_PID$PID3)

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr + PID3 +
                  PID3:pty + PID3:num_const + PID3:seniority + PID3:position + 
                  PID3:cmte + PID3:mov + PID3:PAC +
                  PID3:cosponsor + PID3:cmteldr
                , data = amce_data_PID, design = generic_design,
                respondent.id = "respondent", respondent.varying = "PID3")
summary(results)

d <- plot.amce.resp.int(results, ci=0.9991667, facet.names = "PID3")
cbind(round(d[which(d$facet=="Conditional on\nPID3 = Democrat" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nPID3 = Democrat" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="Conditional on\nPID3 = Independent" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nPID3 = Independent" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="Conditional on\nPID3 = Republican" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nPID3 = Republican" & d$pe<100),c(5,7)])

# Table A.15 (government experience conditional effects)

amce_data_bin$prev_experience <- ifelse(amce_data_bin$PrevExp_MemCongress!="No" |
                                          amce_data_bin$PrevExp_CongStaff!="No" |
                                          amce_data_bin$PrevExp_PresApp!="No" |
                                          amce_data_bin$PrevExp_Bureaucrat!="No" |
                                          amce_data_bin$PrevExp_EOP!="No" |
                                          amce_data_bin$PrevExp_Other!="No",
                                        "yes", "no")
amce_data_bin$prev_experience <- as.factor(amce_data_bin$prev_experience)

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr + prev_experience +
                  prev_experience:pty + prev_experience:num_const + prev_experience:seniority + 
                  prev_experience:position + 
                  prev_experience:cmte + prev_experience:mov + prev_experience:PAC +
                  prev_experience:cosponsor + prev_experience:cmteldr
                , data = amce_data_bin, design = generic_design,
                respondent.id = "respondent", respondent.varying = "prev_experience")
summary(results)

d <- plot.amce.resp.int(results, ci=0.99875, facet.names = "prev_experience")
cbind(round(d[which(d$facet=="Conditional on\nprev_experience = yes" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nprev_experience = yes" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="Conditional on\nprev_experience = no" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nprev_experience = no" & d$pe<100),c(5,7)])

# Table A.16 (lobbying experience conditional effects)

amce_data_lob_exp <- amce_data_bin[which(!is.na(amce_data_bin$Years_Experience)),]
amce_data_lob_exp$vet_lob <- as.factor(ifelse(amce_data_lob_exp$Years_Experience=="16-20 years" |
                                       amce_data_lob_exp$Years_Experience=="More than 20 years", "Veteran",
                                                                                                 "Not Veteran"))

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                   cosponsor + cmteldr + vet_lob +
                   vet_lob:pty + vet_lob:num_const + vet_lob:seniority + 
                   vet_lob:position + 
                   vet_lob:cmte + vet_lob:mov + vet_lob:PAC +
                   vet_lob:cosponsor + vet_lob:cmteldr
                , data = amce_data_lob_exp, design = generic_design,
                respondent.id = "respondent", respondent.varying = "vet_lob")
summary(results)

d <- plot.amce.resp.int(results, ci=0.99875, facet.names = "vet_lob")
cbind(round(d[which(d$facet=="Conditional on\nvet_lob = Veteran" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nvet_lob = Veteran" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="Conditional on\nvet_lob = Not Veteran" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\nvet_lob = Not Veteran" & d$pe<100),c(5,7)])

# Table A.17 (lobbyist/policymaker generational alignment effects)

amce_data_align <- amce_data_bin[which(!is.na(amce_data_bin$Age)),]
amce_data_align$gen_align <- as.factor(ifelse((amce_data_align$Age=="50-64" & (amce_data_align$seniority=="3 terms"|
                                                                                  amce_data_align$seniority=="7 terms"))
                                              | (amce_data_align$Age=="65 or over" & (amce_data_align$seniority=="3 terms"|
                                                                                         amce_data_align$seniority=="7 terms"))
                                              | (amce_data_align$Age=="18-29" & (amce_data_align$seniority=="Freshman"|
                                                                                         amce_data_align$seniority=="1 term"))
                                              | (amce_data_align$Age=="30-49" & (amce_data_align$seniority=="Freshman"|
                                                                                    amce_data_align$seniority=="1 term")), 
                                              "Aligned",
                                              "Not Aligned"))

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                   cosponsor + cmteldr + gen_align +
                   gen_align:pty + gen_align:num_const + gen_align:seniority + 
                   gen_align:position + 
                   gen_align:cmte + gen_align:mov + gen_align:PAC +
                   gen_align:cosponsor + gen_align:cmteldr
                , data = amce_data_align, design = generic_design,
                respondent.id = "respondent", respondent.varying = "gen_align")
summary(results)

d <- plot.amce.resp.int(results, ci=0.99875, facet.names = "gen_align")
cbind(round(d[which(d$facet=="Conditional on\ngen_align = Aligned" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\ngen_align = Aligned" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="Conditional on\ngen_align = Not Aligned" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="Conditional on\ngen_align = Not Aligned" & d$pe<100),c(5,7)])

# Table A.18 (attribute-by-attribute conditional effects: position)

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr +
                  position:num_const + position:seniority + position:pty + 
                  position:cmte + position:mov + position:PAC +
                  position:cosponsor + position:cmteldr
                , data = amce_data_bin, design = generic_design,
                respondent.id = "respondent")
summary(results)
d <- plot.amce.attr.int(results, ci=0.9989583, facet.names = "position")
cbind(round(d[which(d$facet=="ACIE\nposition = Undeclared" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nposition = Undeclared" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="ACIE\nposition = Support" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nposition = Support" & d$pe<100),c(5,7)])

# Table A.19 (attribute-by-attribute conditional effects: cosponsorship)

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr +
                  cosponsor:num_const + cosponsor:seniority + cosponsor:pty + 
                  cosponsor:cmte + cosponsor:mov + cosponsor:PAC +
                  cosponsor:position + cosponsor:cmteldr
                , data = amce_data_bin, design = generic_design,
                respondent.id = "respondent")
summary(results)
d <- plot.amce.attr.int(results, ci=0.996875, facet.names = "cosponsor")
cbind(round(d[which(d$facet=="ACIE\ncosponsor = Yes" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\ncosponsor = Yes" & d$pe<100),c(5,7)])

# Table A.20 (attribute-by-attribute conditional effects: party)

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr +
                  pty:num_const + pty:seniority + pty:position + 
                  pty:cmte + pty:mov + pty:PAC +
                  pty:cosponsor + pty:cmteldr
                , data = amce_data_bin, design = generic_design,
                respondent.id = "respondent")
summary(results)
d <- plot.amce.attr.int(results, ci=0.996875, facet.names = "pty")
cbind(round(d[which(d$facet=="ACIE\npty = Democrat" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\npty = Democrat" & d$pe<100),c(5,7)])

# Table A.21 (attribute-by-attribute conditional effects: committee membership)

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr +
                  cmte:num_const + cmte:seniority + cmte:position + 
                  cmte:pty + cmte:mov + cmte:PAC +
                  cmte:cosponsor + cmte:cmteldr
                , data = amce_data_bin, design = generic_design,
                respondent.id = "respondent")
summary(results)
d <- plot.amce.attr.int(results, ci=0.996875, facet.names = "cmte")
cbind(round(d[which(d$facet=="ACIE\ncmte = Yes" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\ncmte = Yes" & d$pe<100),c(5,7)])

# Table A.22 (attribute-by-attribute conditional effects: committee leadership)

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr +
                  cmteldr:num_const + cmteldr:seniority + cmteldr:position + 
                  cmteldr:pty + cmteldr:mov + cmteldr:PAC +
                  cmteldr:cosponsor + cmteldr:cmte
                , data = amce_data_bin, design = generic_design,
                respondent.id = "respondent")
summary(results)
d <- plot.amce.attr.int(results, ci=0.9988889, facet.names = "cmteldr")
cbind(round(d[which(d$facet=="ACIE\ncmteldr = Subcommittee ranking member" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\ncmteldr = Subcommittee ranking member" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="ACIE\ncmteldr = Subcommittee chair" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\ncmteldr = Subcommittee chair" & d$pe<100),c(5,7)])

# Table A.23 (attribute-by-attribute conditional effects: seniority)

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr +
                  seniority:num_const + seniority:position + seniority:pty + 
                  seniority:cmte + seniority:mov + seniority:PAC +
                  seniority:cosponsor + seniority:cmteldr
                , data = amce_data_bin, design = generic_design,
                respondent.id = "respondent")
summary(results)
d <- plot.amce.attr.int(results, ci=0.9992188, facet.names = "seniority")
cbind(round(d[which(d$facet=="ACIE\nseniority = 1 term" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nseniority = 1 term" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="ACIE\nseniority = 3 terms" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nseniority = 3 terms" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="ACIE\nseniority = 7 terms" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nseniority = 7 terms" & d$pe<100),c(5,7)])

# Table A.24 (attribute-by-attribute conditional effects: margin of victory)

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr +
                  mov:seniority + mov:position + mov:pty + 
                  mov:cmte + mov:num_const + mov:PAC +
                  mov:cosponsor + mov:cmteldr
                , data = amce_data_bin, design = generic_design,
                respondent.id = "respondent")
summary(results)
d <- plot.amce.attr.int(results, ci=0.9992188, facet.names = "mov")
cbind(round(d[which(d$facet=="ACIE\nmov = 10% to 20%" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nmov = 10% to 20%" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="ACIE\nmov = 20% to 30%" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nmov = 20% to 30%" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="ACIE\nmov = More than 30%" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nmov = More than 30%" & d$pe<100),c(5,7)])

# Table A.25 (attribute-by-attribute conditional effects: constituency strength)

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr +
                  num_const:seniority + num_const:position + num_const:pty + 
                  num_const:cmte + num_const:mov + num_const:PAC +
                  num_const:cosponsor + num_const:cmteldr
                , data = amce_data_bin, design = generic_design,
                respondent.id = "respondent")
summary(results)
d <- plot.amce.attr.int(results, ci=0.9992188, facet.names = "num_const")
cbind(round(d[which(d$facet=="ACIE\nnum_const = Low" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nnum_const = Low" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="ACIE\nnum_const = Moderate" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nnum_const = Moderate" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="ACIE\nnum_const = High" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nnum_const = High" & d$pe<100),c(5,7)])

# Table A.26 (attribute-by-attribute conditional effects: campaign contributions)

results <- amce(choice ~ pty + num_const + seniority + position + cmte + mov + PAC +
                  cosponsor + cmteldr +
                  PAC:seniority + PAC:position + PAC:pty + 
                  PAC:cmte + PAC:num_const + PAC:mov +
                  PAC:cosponsor + PAC:cmteldr
                , data = amce_data_bin, design = generic_design,
                respondent.id = "respondent")
summary(results)
d <- plot.amce.attr.int(results, ci=0.9992188, facet.names = "PAC")
cbind(round(d[which(d$facet=="ACIE\nPAC = Yes; $100" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nPAC = Yes; $100" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="ACIE\nPAC = Yes; $500" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nPAC = Yes; $500" & d$pe<100),c(5,7)])
cbind(round(d[which(d$facet=="ACIE\nPAC = Yes; $1000" & d$pe<100),c(1:4)], 2),
      d[which(d$facet=="ACIE\nPAC = Yes; $1000" & d$pe<100),c(5,7)])
